package com.xiaochangwei.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.xiaochangwei.web.beans.UserInfo;
import com.xiaochangwei.web.service.UserService;

/**
 * @since 2017年3月9日 下午3:49:51
 * @author 肖昌伟 317409898@qq.com
 * @description
 */
@Controller
@RequestMapping("/user")
public class UserController extends BaseController {

	@Autowired
	private UserService userService;

	@RequestMapping(value = "/userDetail.html", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView userCenter(ModelAndView mv, HttpSession session) {
		UserInfo userInfo = userService.getUserInfo(getUserId());
		if (userInfo == null || userInfo.getNickName() == null || userInfo.getBirthDay() == null
				|| userInfo.getCityCode() == null) {
			// 跳转到userinfo更新完善页面
			mv.setViewName("userUpdate");
		} else {
			mv.setViewName("userDetail");
			mv.addObject("userInfo", userInfo);
		}
		return mv;
	}

	@RequestMapping(value = "/updateUserInfo", method = { RequestMethod.POST })
	public ModelAndView updateUserInfo(UserInfo userInfo, ModelAndView mv, HttpSession session,
			HttpServletRequest request) {
		System.out.println(JSON.toJSONString(userInfo));
		if (userInfo.getId() == null) {
			userInfo.setId(getUserId());
			userService.insertUserInfo(userInfo);
		} else {
			// 更新详情
			if (userInfo.getId().equals(getUserId())) {
				userService.updateUserInfo(userInfo);
			}
		}
		mv.addObject("page", "/user/userCenter.html");
		mv.addObject("msg", "更新用户信息成功");
		mv.setViewName("success");
		return mv;
	}

	@RequestMapping(value = "/updateUserContact", method = { RequestMethod.POST })
	public ModelAndView updateUserContact(UserInfo userInfo, ModelAndView mv, HttpSession session,
			HttpServletRequest request) {
		if (userInfo.getId().equals(getUserId())) {
			userService.updateUserContact(userInfo);
		}
		mv.addObject("page", "/user/userDetail.html");
		mv.addObject("msg", "更新用户信息成功");
		mv.setViewName("success");
		return mv;
	}

	@RequestMapping(value = "/{id}/detail.html", method = { RequestMethod.POST })
	public ModelAndView userDetail(@PathVariable("id") Long id, ModelAndView mv, HttpSession session,
			HttpServletRequest request) {
		UserInfo userInfo = userService.getUserInfo(id);
		System.out.println(JSON.toJSONString(userInfo));
		if (userInfo != null) {
			mv.addObject("userInfo", userInfo);
		}
		mv.setViewName("userDetail");
		return mv;
	}

	@ResponseBody
	@RequestMapping(value = "/contact/{id}", method = { RequestMethod.POST })
	public UserInfo userContact(@PathVariable("id") Long id, HttpSession session, HttpServletRequest request) {
		if (id.equals(getUserId()) || hasPermission(id)) {
			UserInfo userInfo = userService.getUserContact(id);
			return userInfo;
		}
		return new UserInfo();
	}

	public boolean hasPermission(Long queryUserId) {
		UserInfo self = userService.getUserInfo(getUserId());
		if (self != null) {
			if (self.getPm().intValue() == 0) {// 无
				return false;
			} else if (self.getPm().intValue() == 3) {// 全国
				return true;
			} else if (self.getPm().intValue() == 1) {// 本市
				UserInfo queryUser = userService.getUserInfo(queryUserId);
				if (queryUser != null && queryUser.getCityCode() != null) {
					if (queryUser.getCityCode().equals(self.getCityCode())) {
						return true;
					}
				}
			} else { // 本省
				UserInfo queryUser = userService.getUserInfo(queryUserId);
				if (queryUser != null && queryUser.getCityCode() != null) {
					if (queryUser.getCityCode().toString().substring(0, 3)
							.equals(self.getCityCode().toString().substring(0, 3))) {
						return true;
					}
				}
			}
		}
		return false;
	}

	@RequestMapping(value = "/recharge.html", method = { RequestMethod.GET })
	public ModelAndView recharge(ModelAndView mv, HttpSession session, HttpServletRequest request) {
		mv.setViewName("recharge");
		return mv;
	}

	@RequestMapping(value = "/updatePm.html", method = { RequestMethod.POST })
	public ModelAndView updatePm(Integer pm, ModelAndView mv, HttpSession session, HttpServletRequest request) {

		// TODO 支付接口调用
		userService.updatePm(getUserId(), pm);
		
		mv.addObject("page", "/user/userDetail.html");
		mv.addObject("msg", "更新用户信息成功");
		mv.setViewName("success");
		return mv;
	}
}
